//
//  AppTools.m
//  Play
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 zt. All rights reserved.
//

#import "AppTools.h"
#import "AFNetworking.h"

@interface AppTools ()<NSURLSessionDelegate>

@end

@implementation AppTools

+(void)getDataWithURL:(NSString *)urlStr Block:(AppToolBlock)block ErrorBlock:(AppToolErrorBlock)errorBlock
{
    NSError *error2 = nil;
    NSString *urlEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
 
    [manager GET:urlEncode parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //block回调
        block(responseObject);
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
        // 存储的沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        // 归档
        [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorBlock(error2);
        NSLog(@"失败==== %@",error);
        // 缓存的文件夹
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        block(result);
        
    }];

   
}

// 创建 Label  遍历构造器
+ (BaseLabel *)creatLabelWithfont:(CGFloat)fontSize
                       numberLine:(NSInteger)num
                    textAlignment:(NSTextAlignment)textAlignment
{
    BaseLabel * label = [[BaseLabel alloc]init];
    label.font = [UIFont systemFontOfSize:fontSize];
    label.numberOfLines = num;
    label.textAlignment = textAlignment;
    return label;
}

+(void)getDataWithFileName:(NSString *)fileName Block:(AppToolBlock)block ErrorBlock:(AppToolErrorBlock)errorBlock;
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
    
    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:filePath];
    
    block(array);

}

#pragma mark -- 时间戳转换标准时间
+ (NSString *)timestampExchange:(NSInteger)time
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}




#pragma mark -- 距离现在时间
+ (NSString *)prettyDateWithReference:(NSDate *)reference {
    NSString *suffix = @" ";
    
    NSDate *date = [NSDate date];
    float different = [reference timeIntervalSinceDate:date];
    if (different < 0) {
        different = -different;
        suffix = @" ";
    }
    
    // days = different / (24 * 60 * 60), take the floor value
    
    
    //相差天数
    float dayDifferent = floor(different / 86400);
    
    int days   = (int)dayDifferent;
        int weeks  = (int)ceil(dayDifferent / 7);
    
        int months = (int)ceil(dayDifferent / 30);
    
        int years  = (int)ceil(dayDifferent / 365);
    
    // It belongs to today
    if (dayDifferent <= 0) {
        // lower than 60 seconds
        if (different < 60) {
            return @"刚刚";
        }
        
        // lower than 120 seconds => one minute and lower than 60 seconds
        if (different < 120) {
            return [NSString stringWithFormat:@"1 分钟前 %@", suffix];
        }
        
        // lower than 60 minutes
        if (different < 660 * 60) {
            return [NSString stringWithFormat:@"%d 分钟 %@", (int)floor(different / 60), suffix];
        }
        
        // lower than 60 * 2 minutes => one hour and lower than 60 minutes
        if (different < 7200) {
            return [NSString stringWithFormat:@"1 小时前 %@", suffix];
        }
        
        // lower than one day
        if (different < 86400) {
            return [NSString stringWithFormat:@"%d 小时 %@", (int)floor(different / 3600), suffix];
        }
    }
    // lower than one week
    else if (days < 7) {
        
        return [NSString stringWithFormat:@"%d 天%@ %@", days, days == 1 ? @"前" : @"", suffix];
    }else{
//        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
//        [formatter setDateStyle:NSDateFormatterMediumStyle];
//        [formatter setTimeStyle:NSDateFormatterShortStyle];
//        [formatter setDateFormat:@"YYYY-MM-dd"];
//        return [NSString stringWithFormat:@"%@",[formatter stringFromDate:reference]];
//    }
        // lager than one week but lower than a month
        
        if (weeks < 4) {
            return [NSString stringWithFormat:@"%d 周%@ %@", weeks, weeks == 1 ? @"前" : @"", suffix];
        }
        // lager than a month and lower than a year
        else if (months < 12) {
            return [NSString stringWithFormat:@"%d 月%@ %@", months, months == 1 ? @"前" : @"", suffix];
        }
        // lager than a year
        else {
            return [NSString stringWithFormat:@"%d 年%@ %@", years, years == 1 ? @"前" : @"", suffix];
        }  
    }
    return self.description;
}





@end
